package com.example.mq2.demo;

import com.example.mq2.common.MqException;
import com.example.mq2.mqclient.Channel;
import com.example.mq2.mqclient.Connection;
import com.example.mq2.mqclient.ConnectionFactory;
import com.example.mq2.mqserver.core.BasicProperties;
import com.example.mq2.mqserver.core.ExchangeType;

import java.io.IOException;

public class Consumer {

    public static void main(String[] args) throws IOException, MqException, InterruptedException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("127.0.0.1");
        connectionFactory.setPort(9090);
        Connection connection = connectionFactory.createConnection();
        Channel channel = connection.createChannel();

        //初始准备工作
        channel.exchangeDeclare("testExchange", ExchangeType.Direct, true, false,
                null);
        channel.queueDeclare("testQueue", true, false, false, null);
        channel.queueBind("testExchange", "testQueue", "testBindKey");

        channel.basicConsumer("testQueue", true, new com.example.mq2.mqserver.core.Consumer() {
            @Override
            public void handlerDeliver(String consumerTag, BasicProperties basicProperties, byte[] body) throws IOException {
                System.out.println("开始消费");
                System.out.println("body:" + new String(body));
                System.out.println("结束消费");
            }
        });

        while(true) {
            Thread.sleep(500);
        }

    }

}
